package com.yuexun.beilunpatient.ui.registration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.adapter.DocSchedualAdapter;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentArrageBean;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentArrageData;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentDepInfoBean;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentDocInfoBean;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentDocInfoData;
import com.yuexun.beilunpatient.ui.registration.bean.ArrageViewData;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentArragePresenter;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentDocInfoPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentArrageView;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentDocInfoView;
import com.yuexun.beilunpatient.widget.FixedSwipeListView;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/18.
 */
public class Act_Registration_DocInfo extends BaseKJActivity implements IAppointmentDocInfoView, IAppointmentArrageView {

    @Bind(R.id.doctor_pic)
    ImageView doctorPic;
    @Bind(R.id.doc_name)
    TextView docName;
    @Bind(R.id.doc_position)
    TextView docPosition;
    @Bind(R.id.doc_department)
    TextView docDepartment;
    @Bind(R.id.doc_intro)
    TextView docIntro;
    @Bind(R.id.doc_schedual)
    FixedSwipeListView docSchedual;

    DocSchedualAdapter adapter;
    List<ArrageViewData> datas = new ArrayList<>();
    AppointmentDocInfoBean docInfoBean;

    AppointmentDocInfoPresenter presenter;
    AppointmentArragePresenter arragePresenter;
    private String doctorId;
    LoadingDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_registration_docinfo);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        doctorId = getIntent().getExtras().getString("doctorId");
        presenter = new AppointmentDocInfoPresenter(new RegistrationModel(), this);
        arragePresenter = new AppointmentArragePresenter(new RegistrationModel(), this);

        if(getIntent().getExtras().getString("type").equals("dep")){
            Map<String, String> params = new HashMap<>();
            params.put("departmentid", getIntent().getExtras().getString("depId"));
            params.put("hospitalId", getIntent().getExtras().getString("hosId"));
            params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            arragePresenter.getAppointmentDepinfo(params);
        }else{
            Map<String, String> params = new HashMap<>();
            params.put("doctorId", doctorId);
            params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            presenter.getAppointmentDocInfo(params);
        }

        dialog = new LoadingDialog(aty);
        dialog.show();

        adapter = new DocSchedualAdapter(docSchedual, datas, R.layout.item_doc_schedual);
        docSchedual.setAdapter(adapter);

        adapter.setListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                ArrageViewData item = datas.get(bundle.getInt("position"));
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("registrationInfo", item);
                if(getIntent().getExtras().getString("type").equals("dep")){
                    bundle1.putString("docname", "");
                    bundle1.putString("docdep", docName.getText().toString());
                }else{
                    bundle1.putString("docname", docName.getText().toString());
                    bundle1.putString("docdep", docDepartment.getText().toString());
                }
                Intent intent = new Intent(aty, Act_Registration_Submit.class);
                intent.putExtras(bundle1);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    public void getAppointmentDocInfo(BaseEntity<AppointmentDocInfoBean> baseEntity) {
        dialog.dismiss();
        if(baseEntity.getStatus().equals("200")){
            docInfoBean = baseEntity.getDatas().get(0);
            docName.setText(docInfoBean.getDoctor_name());
            docPosition.setText(docInfoBean.getDoctor_title());
            docDepartment.setText(docInfoBean.getDepartment_name());
            docIntro.setText(docInfoBean.getDoctor_introduction());
            Glide.with(aty).load(docInfoBean.getDoctor_photo()).into(doctorPic);

            for(AppointmentDocInfoData docInfoData : docInfoBean.getList()){
                ArrageViewData viewData = new ArrageViewData();
                viewData.setClinic_fee(docInfoData.getClinic_fee());
                viewData.setOut_time(docInfoData.getOut_time());
                viewData.setRated_num(docInfoData.getRated_num());
                viewData.setReg_fee(docInfoData.getReg_fee());
                viewData.setSchedule_date(docInfoData.getSchedule_date());
                viewData.setSchedule_num(docInfoData.getSchedule_num());
                datas.add(viewData);
            }
            adapter.refresh(datas);
        }
    }

    @Override
    public void getAppointmentArrageList(BaseEntity<AppointmentArrageBean> baseEntity) {
        dialog.dismiss();
        if(baseEntity.getStatus().equals("200")){
            for(AppointmentArrageData docInfoData : baseEntity.getDatas().get(0).getList()){
                ArrageViewData viewData = new ArrageViewData();
                viewData.setClinic_fee(docInfoData.getClinic_fee());
                viewData.setOut_time(docInfoData.getOut_time());
                viewData.setRated_num(docInfoData.getRated_num());
                viewData.setReg_fee(docInfoData.getReg_fee());
                viewData.setSchedule_date(docInfoData.getSchedule_date());
                viewData.setSchedule_num(docInfoData.getSchedule_num());
                datas.add(viewData);
            }
            adapter.refresh(datas);
        }
    }

    @Override
    public void getAppointmentDepinfo(BaseEntity<AppointmentDepInfoBean> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            AppointmentDepInfoBean depInfoBean = baseEntity.getDatas().get(0);
            docName.setText(depInfoBean.getDepartment_name());
            docPosition.setText(depInfoBean.getDepartment_type());
            docDepartment.setText(depInfoBean.getDepartment_location());
            docIntro.setText(depInfoBean.getDepartment_introduction());

            Map<String, String> params = new HashMap<>();
            params.put("departmentId", getIntent().getExtras().getString("depId"));
            params.put("hospitalId", getIntent().getExtras().getString("hosId"));
            params.put("state", "1");//是否合并上下午 1合并，空或其他不合并
            params.put("areaCode", "330206");
            params.put("hospitalRegStatus", "Y");
            params.put("scheduleBegdate", getIntent().getExtras().getString("selectDate"));
            params.put("scheduleEnddate", getIntent().getExtras().getString("selectDate"));
            params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            arragePresenter.getAppointmentArrageList(params);

        }else{
            dialog.dismiss();
        }
    }
}
